package com.qin.malldiscount.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.domain.SmsHomeBrand;
import com.qin.malldiscount.service.SmsHomeBrandService;
import com.qin.malldiscount.mapper.SmsHomeBrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【sms_home_brand(首页推荐品牌表)】的数据库操作Service实现
* @createDate 2022-11-24 10:42:56
*/
@Service
public class SmsHomeBrandServiceImpl extends ServiceImpl<SmsHomeBrandMapper, SmsHomeBrand>
    implements SmsHomeBrandService{

    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;
    
    /**
     * 添加首页品牌推荐
     */
    @Override
    public int create(List<SmsHomeBrand> homeBrandList) {
        for (SmsHomeBrand smsHomeBrand : homeBrandList ) {
            //开启推荐
            smsHomeBrand.setRecommendStatus(1);
            smsHomeBrand.setSort(0);
            int count = homeBrandMapper.insert(smsHomeBrand);
        }
        return homeBrandList.size();
    }

    /**
     * 修改品牌推荐排序
     */
    @Override
    public int updateSort(Long id, Integer sort) {
        SmsHomeBrand homeBrand = new SmsHomeBrand();
        homeBrand.setId(id);
        homeBrand.setSort(sort);
        int count = homeBrandMapper.updateById(homeBrand);
        return count;
    }

    /**
     * 批量删除品牌推荐
     */
    @Override
    public int delete(List<Long> ids) {
        int count = homeBrandMapper.deleteBatchIds(ids);
        return count;
    }

    /**
     * 更新推荐状态
     */
    @Override
    public int updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        QueryWrapper<SmsHomeBrand> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        SmsHomeBrand record = new SmsHomeBrand();
        record.setRecommendStatus(recommendStatus);
        int count = homeBrandMapper.update(record, wrapper);
        return count;
    }

    /**
     * 分页查询品牌推荐
     */
    @Override
    public Page<SmsHomeBrand> list(String brandName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        Page<SmsHomeBrand> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SmsHomeBrand> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(brandName)){
            wrapper.like("brand_name",brandName);
        }
        if (recommendStatus!=null){
            wrapper.eq("recommend_status",recommendStatus);
        }
        wrapper.orderByDesc("sort");
        Page<SmsHomeBrand> list = homeBrandMapper.selectPage(page, wrapper);
        return list;
    }
}




